(function () {
    var io = ppt.io;
    var ENTRANCE = ["1v1", "2v2"];

    function Matching() {
        Matching.super(this);

        this.ASSETS = {
            RES: ASSETS.MATCH_WAIT,
            TYPE: "simple"
        };

        this.BG = null;
        this.content = null;
        this.entrance = [];
        this.quickStart = null;

        this.init();
    }
    Laya.class(Matching, "Scene.Matching", Laya.Sprite);
    var _proto = Matching.prototype;
    _proto.init = function () {
        this.size(GAME_CONFIG.WIDTH, Laya.stage.height);

        io.register("pvp::wait", this, this.gotoWaiting);
        io.register("game::enter", this, this.update);
    }
    _proto.gotoWaiting = function (data) {
        if (data.code == 107) {
            var config = {
                msg: data.msg,
                close: true,
                btn: {
                    btnCancel: {
                        skin: "cancel"
                    },
                    btnConfirm: {
                        skin: "confirm"
                    }
                }
            }
            ppt.director.popScene(new Alert.Public(config));
            
            return;
        }
        ppt.director.runScene(new Scene.Waiting(data));
    }
    _proto.update = function (data) {
        this.quickStart.update(data);
    }

    _proto.onAdd = function () {
        // var filter = new Laya.BlurFilter();
        //     filter.strength = 5;

        var BG = utils.createSkeleton("res/public/public_bg");
            BG.pos(GAME_CONFIG.WIDTH / 2, Laya.stage.height / 2);
            BG.play(0, true);
            // BG.filters = [filter];

        var content = new ui.Matching.MatchingBGUI();
            content.centerX = 0;
            content.centerY = 0;
            content.light.play(0, true);

        for (var i = 0; i < 2; i++) {
            var entrance = new Com.Matching.Entrance(ENTRANCE[i]);

            content.addChild(entrance);
            this.entrance.push(entrance);
        }

        var quickStart = new Com.Matching.QuickStart();

        this.BG = BG;
        this.content = content;
        this.quickStart = quickStart;

        this.addChildren(BG, content, quickStart);
    }
    _proto.onEnter = function () {
        ppt.header.enter("matching");

        for (var i in this.entrance) {
            this.entrance[i].enter();
        }

        this.quickStart.enter();
    }
    _proto.onExit = function () {
        this.destroy(true);

        io.unregister("pvp::wait", this.gotoWaiting);
        io.unregister("game::enter", this.update);

        this.BG = null;
        this.content = null;
        this.entrance = null;
        this.quickStart = null;
    }
    _proto.onResize = function (height) {
        console.log("Matching Resize");
        this.height = height;
        this.BG.y = height / 2;
        this.content.centerY = 0;
        this.quickStart.resize(height);
    }
})();